package com.shop.servlet.product;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;


public class AddImage extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public AddImage() {
        super();
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("进来");
		request.setCharacterEncoding("UTF-8");
		response.setHeader("Content-Type","text/html;charset=utf-8");
		response.setContentType("text/html;charset=utf-8");	

			FileItemFactory factory = new DiskFileItemFactory();
			 
	         // 文件上传处理器
	         ServletFileUpload upload = new ServletFileUpload(factory);
	 
	         // 解析请求信息
	         List items = null;
	         try {
	             items = upload.parseRequest(request);
	         }
	         catch (FileUploadException e) {
	             e.printStackTrace();
	         }
	 
	         // 对请求信息进行判断
	         Iterator<Object> iter = items.iterator();
	         while (iter.hasNext()) {
	             FileItem item = (FileItem) iter.next();
	             // 信息为普通的格式
	             if (item.isFormField()) {
	                 String fieldName = item.getFieldName();
	                 String value = item.getString();
	                 request.setAttribute(fieldName, value);
	             }
	             // 信息为文件格式
	             else {
	            	String fileName = item.getName();//获得上传图片的名称
	                 int index = fileName.lastIndexOf("\\");
	                 fileName = fileName.substring(index + 1);
	                 request.setAttribute("realFileName", fileName);
	                 String basePath =("D:\\Workspace\\ShopSystem-master\\WebContent\\image");
	                 System.out.println(basePath);//打印当前位置
	                 File file = new File(basePath, fileName);
	                 try {
	                	 
	                     item.write(file);
	                 }
	                 catch (Exception e) {
	                     e.printStackTrace();
	                 }
	             }
	         }
	}
}
	            
	         
	 
